//
//  ZGLRegisterView.m
//  ZeegingLottery
//
//  Created by 蒂埃里 on 2018/4/18.
//  Copyright © 2018年 蒂埃里. All rights reserved.
//

#import "ZGLRegisterView.h"

@interface ZGLRegisterView ()<UITextFieldDelegate>
{
    CGFloat offSet;
    CGFloat duration;
    CGRect keyboardFrame;
}

@property (nonatomic, strong)UIButton *nextInKeyboardButton;

@end

@implementation ZGLRegisterView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame:frame]) {
        
        self.backgroundColor = ViewBackgroundColor;
        
        //即将显示的键盘
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
        //即将推出的键盘
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        
        [self setupUI];
        
    }
    
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - method

- (void)nextTextField
{
    DLog(@"下一项");
    if (_accountTextField.isFirstResponder) {
        
        [_VerificationCodeTF becomeFirstResponder];
    }else if (_VerificationCodeTF.isFirstResponder){
        
        [_passwordTF becomeFirstResponder];
    }else if (_passwordTF.isFirstResponder){
        
        [_confirmPasswordTF becomeFirstResponder];
    }
    
    
}

- (void)prevTextField
{
    DLog(@"上一项"); 
    if (_VerificationCodeTF.isFirstResponder) {
        
        [UIView animateWithDuration:duration animations:^{
            
            self.frame = CGRectMake(0, STATUEHEIGHT, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT);
        }];
        [_accountTextField becomeFirstResponder];
        
        
    }else if (_passwordTF.isFirstResponder){
        
        [_VerificationCodeTF becomeFirstResponder];
    }else if (_confirmPasswordTF.isFirstResponder){
        
        [_passwordTF becomeFirstResponder];
    }
    
}

- (void)doneBtnClick
{
    DLog(@"完成");
    [super endEditing:YES];
    [self endEditing:YES];
    
}


#pragma mark - setupUI
- (void)setupUI
{
    [self addSubview:self.tipLabel];
    [self addSubview:self.accountTextField];
    [self addSubview:self.passwordTF];
    [self addSubview:self.confirmPasswordTF];
    [self addSubview:self.registeButton];
    [self addSubview:self.getVerificationCodeBtn];
    [self addSubview:self.VerificationCodeTF];
    
    [_tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(40);
        make.height.mas_equalTo(22);
        make.left.equalTo(self).offset(20);
        make.right.equalTo(self).offset(-20);
        
    }];
    
    [_getVerificationCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(_tipLabel.mas_right);
        make.top.equalTo(_tipLabel.mas_bottom).offset(40);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(35);
        
    }];
    
    
    [_accountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_tipLabel);
        make.right.equalTo(_getVerificationCodeBtn.mas_left);
        make.height.mas_equalTo(40);
        make.centerY.equalTo(_getVerificationCodeBtn);
        
    }];
    
    UIView *line1 = [[UIView alloc]init];
    line1.backgroundColor = BorderColor;
    [self addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_accountTextField);
        make.right.equalTo(_accountTextField);
        make.height.equalTo(@(kBorderWidth));
        make.top.equalTo(_accountTextField.mas_bottom).offset(1);
        
    }];
    
    
    [_VerificationCodeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_tipLabel);
        make.right.equalTo(_getVerificationCodeBtn);
        make.height.equalTo(_accountTextField);
        make.top.equalTo(_accountTextField.mas_bottom).offset(10);
        
    }];
    
    UIView *line2 = [[UIView alloc]init];
    line2.backgroundColor = BorderColor;
    [self addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_VerificationCodeTF);
        make.right.equalTo(_VerificationCodeTF);
        make.height.equalTo(@(kBorderWidth));
        make.top.equalTo(_VerificationCodeTF.mas_bottom).offset(1);
        
    }];
    
    [_passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_accountTextField);
        make.right.equalTo(_getVerificationCodeBtn);
        make.height.equalTo(_accountTextField);
        make.top.equalTo(_VerificationCodeTF.mas_bottom).offset(10);
        
    }];
    
    UIView *line3 = [[UIView alloc]init];
    line3.backgroundColor = BorderColor;
    [self addSubview:line3];
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_VerificationCodeTF);
        make.right.equalTo(_VerificationCodeTF);
        make.height.equalTo(@(kBorderWidth));
        make.top.equalTo(_passwordTF.mas_bottom).offset(1);
        
    }];
    
    [_confirmPasswordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_passwordTF);
        make.right.equalTo(_passwordTF);
        make.height.equalTo(_passwordTF);
        make.top.equalTo(_passwordTF.mas_bottom).offset(10);
        
    }];
    
    UIView *line4 = [[UIView alloc]init];
    line4.backgroundColor = BorderColor;
    [self addSubview:line4];
    [line4 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_VerificationCodeTF);
        make.right.equalTo(_VerificationCodeTF);
        make.height.equalTo(@(kBorderWidth));
        make.top.equalTo(_confirmPasswordTF.mas_bottom).offset(1);
        
    }];
    
    
    [_registeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_passwordTF);
        make.right.equalTo(_passwordTF);
        make.height.mas_equalTo(45);
        make.top.equalTo(_confirmPasswordTF.mas_bottom).offset(50);
        
    }];
    
}

#pragma mark - getter

-(UITextField *)VerificationCodeTF
{
    if (!_VerificationCodeTF) {
        _VerificationCodeTF = [[UITextField alloc]init];
        _VerificationCodeTF.textColor = mainTextColor;
        _VerificationCodeTF.placeholder = @"短信验证码";
        _VerificationCodeTF.delegate = self;
        _VerificationCodeTF.keyboardType = UIKeyboardTypeNumberPad;
        _VerificationCodeTF.returnKeyType = UIReturnKeyNext;
        _VerificationCodeTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _VerificationCodeTF.inputAccessoryView = [self addToolbar];
    }
    
    return _VerificationCodeTF;
}

-(UIButton *)getVerificationCodeBtn
{
    if (!_getVerificationCodeBtn) {
        _getVerificationCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_getVerificationCodeBtn setBackgroundColor:[UIColor whiteColor]];
        [_getVerificationCodeBtn setTitleColor:globalRed forState:UIControlStateNormal];
        _getVerificationCodeBtn.layer.borderWidth = kBorderWidth;
        _getVerificationCodeBtn.layer.borderColor = globalRed.CGColor;
        _getVerificationCodeBtn.layer.cornerRadius = 5;
        _getVerificationCodeBtn.layer.masksToBounds = YES;
        _getVerificationCodeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [_getVerificationCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
    
    return _getVerificationCodeBtn;
}

-(UILabel *)tipLabel
{
    if (!_tipLabel) {
        _tipLabel = [[UILabel alloc]init];
        _tipLabel.text = @"请输入信息以完成注册";
        _tipLabel.textColor = [UIColor lightGrayColor];
        _tipLabel.font = [UIFont systemFontOfSize:15];
        _tipLabel.textAlignment = 1;
    }
    
    return _tipLabel;
}

-(UITextField *)accountTextField
{
    if (!_accountTextField) {
        _accountTextField = [[UITextField alloc]init];
        _accountTextField.textColor = mainTextColor;
        _accountTextField.placeholder = @"请输入手机号";
        _accountTextField.delegate = self;
        _accountTextField.keyboardType = UIKeyboardTypeNumberPad;
        _accountTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _accountTextField.returnKeyType = UIReturnKeyNext;
        _accountTextField.inputAccessoryView = [self addToolbar];
    }
    
    return _accountTextField;
}

-(UITextField *)passwordTF
{
    if (!_passwordTF) {
        _passwordTF = [[UITextField alloc]init];
        _passwordTF.textColor = mainTextColor;
        _passwordTF.placeholder = @"请输入密码";
        _passwordTF.delegate = self;
        _passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _passwordTF.secureTextEntry = YES;
        _passwordTF.returnKeyType = UIReturnKeyNext;
        _passwordTF.inputAccessoryView = [self addToolbar];
    }
    
    return _passwordTF;
}

-(UITextField *)confirmPasswordTF
{
    if (!_confirmPasswordTF) {
        _confirmPasswordTF = [[UITextField alloc]init];
        _confirmPasswordTF.textColor = mainTextColor;
        _confirmPasswordTF.placeholder = @"请确认密码";
        _confirmPasswordTF.delegate = self;
        _confirmPasswordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _confirmPasswordTF.secureTextEntry = YES;
        _confirmPasswordTF.returnKeyType = UIReturnKeyDone;//需要实现代理方法
        _confirmPasswordTF.inputAccessoryView = [self addToolbar];
    }
    
    return _confirmPasswordTF;
}

-(UIButton *)registeButton
{
    if (!_registeButton) {
        _registeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_registeButton setTitle:@"注册" forState:UIControlStateNormal];
        _registeButton.titleLabel.textColor = [UIColor whiteColor];
        _registeButton.backgroundColor = globalRed;
        _registeButton.layer.cornerRadius = 5;
        _registeButton.layer.masksToBounds = YES;
//        [_registeButton addTarget:self action:@selector(registerBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _registeButton;
}

- (UIToolbar *)addToolbar
{
    UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 35)];
    toolbar.tintColor = globalRed;
    toolbar.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *nextButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"jianTouDown"] style:UIBarButtonItemStylePlain target:self action:@selector(nextTextField)];
    UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedSpace.width = 30;
    UIBarButtonItem *prevButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"jianTouUp"] style:UIBarButtonItemStylePlain target:self action:@selector(prevTextField)];
    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(doneBtnClick)];
    toolbar.items = @[nextButton,fixedSpace,prevButton,flexibleSpace,doneButton];
    
    
    
    
    return toolbar;
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super endEditing:YES];
    [self endEditing:YES];
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    DLog(@"%@",textField.placeholder);
    
    return YES;
}

#pragma mark - notification meth

- (void)keyboardWillShow:(NSNotification *)notifcation
{
    NSDictionary *dict = notifcation.userInfo;
    keyboardFrame = [dict[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
    duration = [[dict objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGFloat keybH = keyboardFrame.size.height;
    CGRect registerBtnF = self.registeButton.frame;
    CGFloat registerBtnMaxH = CGRectGetMaxY(registerBtnF);
    offSet = (registerBtnMaxH + keybH) - UI_SCREEN_HEIGHT;
    
    if (IS_IPHONE_4and5 && !_accountTextField.isFirstResponder) {
        
        
        [UIView animateWithDuration:duration animations:^{
            
            self.frame = CGRectMake(0, -offSet, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT);
        }];
    }

}

- (void)keyboardWillHide:(NSNotification *)notification
{
    DLog(@"隐藏键盘");
    
    if (IS_IPHONE_4and5 ) {
        
        [UIView animateWithDuration:duration animations:^{
            
            self.frame = CGRectMake(0, STATUEHEIGHT, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT);
        }];
    }

}


@end
